package com.yy.jhl.ui.manager.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.Base64;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonutils.TimeUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.FileInfo;
import com.yy.jhl.bean.LogisticsInfo;
import com.yy.jhl.bean.ProductInfo;
import com.yy.jhl.interfaces.FileUploadCallBack;
import com.yy.jhl.ui.main.activity.TakePhotoUploadActivity;
import com.yy.jhl.ui.manager.contract.OutboundCheckContract;
import com.yy.jhl.ui.manager.contract.OutboundSubmitContract;
import com.yy.jhl.ui.manager.presenter.OutboundCheckPresenter;
import com.yy.jhl.ui.manager.presenter.OutboundSubmitPresenter;
import com.yy.jhl.util.DataRequestUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.yy.jhl.ui.main.activity.TakePhotoUploadActivity.DO_BIG_IMAGE;
import static com.yy.jhl.ui.main.activity.TakePhotoUploadActivity.UPLOAD_IMG;

/**
 * Created by libingn on 2017/12/11.
 */

public class OutboundAfterActivity extends BaseActivity<OutboundCheckPresenter> implements OutboundCheckContract.View {
    @BindView(R.id.order_code_text)
    TextView orderCodeText;
    @BindView(R.id.batch_text)
    TextView batchText;
    @BindView(R.id.goods_quantity_text)
    TextView goodsQuantityText;
    @BindView(R.id.order_product_more_text)
    TextView productMoreText;
    @BindView(R.id.goods_num_text)
    TextView goodsNumText;
    @BindView(R.id.add_img)
    ImageView addImg;
    @BindView(R.id.company_name_edit)
    EditText companyNameEdit;
    @BindView(R.id.manager_name_edit)
    EditText managerNameEdit;
    @BindView(R.id.contact_phone_edit)
    EditText contactPhoneEdit;
    @BindView(R.id.note_edit)
    EditText noteEdit;
    @BindView(R.id.time_text)
    TextView timeText;
    @BindView(R.id.upload_btn)
    Button uploadBtn;
    LogisticsInfo logisticsInfo;
    String attachPath = "";
    public static void startAction(Context mContext, LogisticsInfo logisticsInfo) {
        Intent intent = new Intent(mContext, OutboundAfterActivity.class);
        intent.putExtra("logisticsInfo", logisticsInfo);
        mContext.startActivity(intent);
    }
    @Override
    public int getLayoutId() {
        return R.layout.act_outbound_after;
    }

    @Override
    public void initView() {
        logisticsInfo = (LogisticsInfo) getIntent().getSerializableExtra("logisticsInfo");
        showTitle("出库", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        timeText.setText(TimeUtil.formatData(TimeUtil.dateFormatYMD, System.currentTimeMillis()));
        orderCodeText.setText(logisticsInfo.getOrder_sn());
        batchText.setText(logisticsInfo.getBatch_sn());
        String batchInfo = logisticsInfo.getBatch_info().getItems();
        int batchInfoTotal = logisticsInfo.getBatch_info().getTotal();
        if (batchInfoTotal > 2){
            productMoreText.setVisibility(View.VISIBLE);
        }else {
            productMoreText.setVisibility(View.GONE);
        }
        goodsQuantityText.setText(StringUtil.isEmpty(batchInfo) ? "" : batchInfo.replace("--","\n"));
        goodsNumText.setText(logisticsInfo.getPackage_num());
    }
    @OnClick({R.id.add_img, R.id.upload_btn,R.id.order_product_more_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_img:
                startActivityForResult(TakePhotoUploadActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1, 640, 640,true,attachPath), UPLOAD_IMG);
                break;
            case R.id.upload_btn:
                String remark = noteEdit.getText().toString().trim();
                if (StringUtil.isEmpty(remark)){
                    showShortToast("请输入备注信息！");
                    return;
                }
                mPresenter.outboundSubmit(logisticsInfo.getBatch_id(),Integer.parseInt(logisticsInfo.getShip_status()),1,remark);
                break;
            case R.id.order_product_more_text:
                ProductListViewWebViewActivity.startAction(mContext,logisticsInfo.getOrder_id(),logisticsInfo.getBatch_id());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
//                case UPLOAD_IMG:
//                    if (data != null && data.getExtras() != null) {
//                        final Uri avatarUri = data.getExtras().getParcelable("PATH");
//                        if (avatarUri != null) {
//                            String imgPath = avatarUri.getPath();
//                            try {
//                                DataRequestUtil.getInstance(mContext).uploadImage("5", Base64.encodeFromFile(imgPath), new FileUploadCallBack() {
//                                    @Override
//                                    public void success(FileInfo fileInfo) {
//                                        attachPath = fileInfo.getFilePath();
//                                        addImg.setImageURI(avatarUri);
//                                    }
//                                    @Override
//                                    public void fail() {
//                                    }
//                                });
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }
//                    break;
            }
        }
    }
    @Override
    public void outboundSubmitSuccess() {
        finish();
    }

    @Override
    public void outboundSubmitFail() {

    }
}
